<template>
  <div>
    <a-select :mode="multi == true ? 'multiple' : 'default'" style="width: 100%" placeholder="请关联任务"
      @deselect="deselect" v-model="ids" :open="open" :disabled="disabled" @dropdownVisibleChange="selectIds">
      <a-select-option v-for="item in selectedObj" :key="item.id">{{ item.nodeName }}</a-select-option>
    </a-select>
    <select-progress-contract-detail-input-modal ref="inputModal" :urlList="urlList" :multi="multi"
      @choseObj="choseObj"></select-progress-contract-detail-input-modal>
  </div>
</template>

<script>
import SelectProgressContractDetailInputModal from './modules/SelectProgressContractDetailInputModal'
import { getAction } from '@/api/manage'

export default {
  name: 'SelectProcessContractDetailInput',
  components: { SelectProgressContractDetailInputModal },
  props: {
    disabled: {
      type: Boolean,
      required: false,
      default: false
    },
    open: {
      type: Boolean,
      required: false,
      default: false
    },
    value: {
      type: String,
      required: false
    },
    multi: {
      type: Boolean,
      default: false,
      required: false
    },
    urlList: {
      type: String,
      default: '/api/erp/progress/erpProgressTemplate/list',
      required: false
    },
    props: {
      type: Object,
      default: null,
    },
    topid: {
      type: String,
      default: '',
    }
  },
  data() {
    return {
      test: '',
      ids: [],
      selectedObj: [],
      deleteIds: [],
      list: [],
      objs: '',
      url: {
        queryById: '/api/erp/progress/erpProgressTemplate/queryById',
        queryByMainId: '/api/erp/progress/erpProgressTemplateDt/queryByMainId',
        queryListByTaskByIds: '/api/erp/progress/erpProgressTaskNode/queryListByTaskByIds'
      },
    }
  },
  mounted() {
    this.initObj(this.value);

  },
  watch: {
    // value(val) {
    //   this.initObj(val);
    // },
  },
  model: {
    prop: 'value',
    event: 'change'
  },
  methods: {
    getOld(id) {
      this.$refs.inputModal.geterpProgressTaskList(id)
    },
    initObj(ids) {
      console.log(ids);
      if(ids!=undefined) {
        if (!ids) {
          return
        }
        let param = {
          taskNodeIds: ids
        }
        getAction(this.url.queryListByTaskByIds, param).then((res) => {
          if (res.success) {
            console.log("huixai==========", res);
            this.selectedObj = res.result || []
            // this.$refs.inputModal.add(this.selectedObj, this.ids);
          }
        });
        this.ids = ids.split(",");
      }
      

      // if (this.selectedObj.length == 0&&ids!=undefined) {
      //   this.ids = ids.split(",");
      //   let param = {
      //     taskNodeIds: ids
      //   }
      //   getAction(this.url.queryListByTaskByIds, param).then((res) => {
      //     if (res.success) {
      //       console.log("huixai==========", res);
      //       this.selectedObj = res.result || []
      //       this.$refs.inputModal.edit(this.selectedObj, this.ids);
      //     }
      //   });
      // }
      // this.deleteIds = [];
      // this.selectedObj = [];
      // if (ids && ids.length > 0) {
      //   this.ids = ids.split(',');
      //   let url = this.url.queryById;
      //   let param = { id: ids };
      //   getAction(url, param).then((res) => {
      //     if (res.success) {
      //       if (!Array.isArray(res.result)) {
      //         this.selectedObj.push(res.result);
      //       } else {
      //         for (let i = 0; i < res.result.length; i++) {
      //           this.selectedObj.push(res.result[i]);
      //         }
      //       }
      //       this.$refs.inputModal.edit(res.result, this.ids);
      //     }
      //   });
      // } else {
      //   this.ids = [];
      //   this.$refs.inputModal.edit(null, null);
      // }

    },
    resetObj() {
      this.userType = false;
      this.ids = [];
      this.selectedObj = [];
      this.deleteIds = [];
      this.open = false;
      this.$refs.inputModal.edit(null, null);
    },
    selectIds() {
      if (!this.disabled) {
        // this.deleteId();
        this.$refs.inputModal.add(this.selectedObj, this.ids);
      }
    },
    deselect(value, option) {
      console.log(value, option, "============");
      this.selectedObj = this.selectedObj.filter(function (e) {
        return value != e.id;
      })
      // this.deleteIds.push(value);
      // this.deleteId();
      let list = {
        choseObj: this.selectedObj
      }
      this.choseObj(list);
    },
    deleteId() {
      let that = this;
      this.selectedObj = this.selectedObj.filter(function (e) {
        return that.deleteIds.indexOf(e.id) == -1;
      })
      this.deleteIds = [];
    },
    // 子modal回调
    choseObj(list) {
      console.log(list, "list====");
      // this.selectedObj = []
      let data = list.choseObj
      this.selectedObj = list.choseObj
      this.ids = []
      data.forEach(element => {
        this.ids.push(element.id)
      });
      // return ;
      // this.ids = []
      // this.ids.push(data.id)
      // // 查看模板中所有进度
      // let url = this.url.queryByMainId;
      // let param = { mainId: data.id };
      // getAction(url, param).then((res) => {
      //   if (res.success) {
      //     this.$emit('input', res.result)
      //   } else {
      //     this.$message.warning(res.message)
      //   }
      // });
      this.objs = this.ids.join(',')
      this.$emit('change', this.objs)
      this.$emit('back', this.selectedObj)
    },
  }
}
</script>

<style scoped>
</style>